/*
 * @Author: 13938960424 1758142861@qq.com
 * @Date: 2023-10-27 16:59:44
 * @LastEditors: 13938960424 1758142861@qq.com
 * @LastEditTime: 2023-11-06 12:26:58
 * @FilePath: /sjf/new-xshell/muduo/test/server.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include "../server/muduo_server.hpp"
#include <stdlib.h>
#include <unordered_map>

// //管理所有的连接
// std::unordered_map<uint64_t,PtrConnection> _conns;
// uint64_t _conn_id = 0;
// EventLoop base_loop;
// LoopThreadPool* loop_pool;
// void CoonectionDestory(const PtrConnection& conn)
// {
//     _conns.erase(conn->Id());
// }
// void OnConnected(const PtrConnection& conn)
// {
//     DBG_LOG("new connection:%p",conn.get());
// }
// void OnMessage(const PtrConnection &conn,Buffer* buf)
// {
//     DBG_LOG("%s",buf->ReadPosition());
//     std::cout << buf->ReadPosition() << std::endl;
//     buf->MoveReadOffset(buf->ReadAblesSize());
//     std::string str = "hello connection";
//     conn->Send(str.c_str(),str.size());
//     //conn->ShutDown();
// }
// void NewAccepter(int fd)
// {
//     _conn_id++;
//     PtrConnection conn(new Connection(_conn_id,fd,loop_pool->NextLoop()));
//     conn->SetMessageCallback(std::bind(OnMessage,std::placeholders::_1,std::placeholders::_2));
//     conn->SetSrvClosedCallback(std::bind(CoonectionDestory,std::placeholders::_1));
//     conn->SetConnectedCallback(std::bind(OnConnected,std::placeholders::_1));
//     conn->EnableInactiveRelease(10);//启动非活跃超时
//     conn->Established();//就绪初始化
//     _conns.insert(std::make_pair(_conn_id,conn));
//     DBG_LOG("NEW-----------------:"); 
// }

// int main()
// {
//     loop_pool = new LoopThreadPool(&base_loop);
//     loop_pool->SetThreadCount(2);
//     loop_pool->Create();
//     Acceptor accepter(&base_loop,8000);
//     accepter.SetAcceptCallback(std::bind(NewAccepter,std::placeholders::_1));
//     accepter.Listen();
//     base_loop.Start();
//     return 0;
// }

// void HandleClose(Channel *channel)
// {
//     logMessage(DEBUG,"Refreshed discovery fd:%d Timed out, release has occurred",channel->Fd());
//     channel->Remove();//移除监控
//     delete channel;
// }

// void HandleRead(Channel *channel)
// {
//     int fd = channel->Fd();
//     char buf[1024] = {0};
//     int ret = recv(fd,buf,sizeof(buf) - 1,0);
//     if(ret <= 0)
//     {
//         channel->Remove();
//         return HandleClose(channel);
//     }
//     logMessage(DEBUG,"server_test->HandleRead recv: %s",buf);
//     channel->EnableWrite();    
    
    
// }
// void HandleWrite(Channel* channel)
// {
//     int fd = channel->Fd();
//     const char* data = "一会要下雨了";
//     int ret = send(fd,data,strlen(data),0);
//     if(ret < 0)
//         return HandleClose(channel);
//     channel->DisableWrite();
// }
// void HandleError(Channel *Channel)
// {
//     return HandleClose(Channel);//直接释放
// }
// void HandleEvent(EventLoop *loop,Channel *channel,uint64_t timerid)
// {
//     loop->TimerRefresh(timerid);
//     logMessage(DEBUG,"handleEvent new one");
// }
// void Acceptor_channel(EventLoop *loop,Channel *lst_channel)
// {
//     int fd = lst_channel->Fd();
//     int newfd = accept(fd,nullptr,nullptr);
//     if(newfd < 0)
//         return;
//     uint64_t timerid = rand() % 10000;
//     Channel *channel = new Channel(newfd,loop);
//     channel->SetReadCallback(std::bind(HandleRead,channel));
//     channel->SetWriteCallback(std::bind(HandleWrite,channel));
//     channel->SetErrorCallback(std::bind(HandleError,channel));
//     channel->SetCloseCallback(std::bind(HandleClose,channel));
//     channel->SetEventCallback(std::bind(HandleEvent,loop,channel,timerid));
//     loop->TimerAdd(timerid,10,std::bind(HandleClose,channel));//添加定时任务
//     channel->EnableRead();//启动可读事件监控
// }
// int main()
// {
//     srand(time(NULL));
//     EventLoop loop;
//     Socket lst_sock;
//     lst_sock.CreateServer(8000);
//     Channel channel(lst_sock.Fd(),&loop);
//     channel.SetReadCallback(std::bind(Acceptor_channel,&loop,&channel));
//     channel.EnableRead();
//     while(1)
//     {
//         loop.Start();
//     }
//     lst_sock.Close();
//     return 0;
// }

void OnConnected(const PtrConnection& conn)
{
    DBG_LOG("new connection:%p",conn.get());
}

void OnClose(const PtrConnection& conn)
{
    DBG_LOG("close connection:%p",conn.get());
}

void OnMessage(const PtrConnection &conn,Buffer* buf)
{
    DBG_LOG("%s",buf->ReadPosition());
    
    buf->MoveReadOffset(buf->ReadAblesSize());
    std::string str = "hello connection";
    conn->Send(str.c_str(),str.size());
    //conn->ShutDown();
}

int main()
{
    TcpServer server(8500);
    server.SetThreadCount(2);
    server.EnableInactiveRelease(10);
    server.SetMessageCallback(OnMessage);
    server.SetClosedCallback(OnClose);
    server.SetConnectedCallback(OnConnected);
    server.Start();
    return 0;
}